package cc.comac.mom.datahandlers.entity.config;

import cc.comac.mom.bean.ResultDev;
import cc.comac.mom.bean.config.EquipDataConfigParamBean;
import cc.comac.mom.bean.config.EquipLabelConfigParamBean;
import cc.comac.mom.dao.entity.config.EquipConnectConfigDao;
import cc.comac.mom.dao.entity.config.EquipDataConfigDao;
import cc.comac.mom.dao.entity.config.EquipLabelConfigDao;
import cc.comac.mom.entity.config.EquipConnectConfigEntity;
import cc.comac.mom.entity.config.EquipDataConfigEntity;
import cc.comac.mom.entity.config.EquipLabelConfigEntity;
import cc.comac.mom.redis.subscribe.MomEquipRedisChannel;
import cc.comac.mom.redis.subscribe.SubscribeMessage;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Transactional
public class EquipLabelConfigDataHandler extends ServiceImpl<EquipLabelConfigDao, EquipLabelConfigEntity> {
	@Autowired
	private EquipLabelConfigDao equipLabelConfigDao;
	@Autowired
	private EquipDataConfigDao equipDataConfigDao;
	@Autowired
	private EquipConnectConfigDao equipConnectConfigDao;
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;

	public ResultBean saveLabelConfig(EquipLabelConfigEntity obj) {
		ResultBean resultBean = new ResultBean(true);
		// 校验主数据重复性
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("label_group", obj.getLabelGroup());
		map.put("label_name", obj.getLabelName());
		map.put("row_status", 1);
		map.put("connect_id", obj.getConnectId());
		List<EquipLabelConfigEntity> selectByMap = equipLabelConfigDao.selectByMap(map);
		if (selectByMap.size() > 0) {
			throw new RuntimeException("同一个连接下标签组下：" + obj.getLabelName() + "已经存在");
		}
		baseEntityDataUtil.setBaseEntityData(obj);
		int insert = equipLabelConfigDao.insert(obj);
		if (insert < 1) {
			throw new RuntimeException("新增失败");
		}
		resultBean.setObject(obj);
		this.convertAndSend(obj);
		return resultBean;
	}
	public ResultBean updateLabelConfig(EquipLabelConfigEntity obj) {
		ResultBean resultBean = new ResultBean(true);
		EquipLabelConfigEntity equipDevEntity = equipLabelConfigDao.selectById(obj.getId());
		if (equipDevEntity == null) {
			throw new RuntimeException("修改的数据不存在！");
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("label_group", obj.getLabelGroup());
		map.put("row_status", 1);
		map.put("label_name", obj.getLabelName());
		map.put("connect_id", obj.getConnectId());
		List<EquipLabelConfigEntity> selectByMap = equipLabelConfigDao.selectByMap(map);
		if (selectByMap.size() > 0) {
			for (EquipLabelConfigEntity e : selectByMap) {
				if (obj.getId() != e.getId()) {
					throw new RuntimeException("同一个连接下标签组下：" + obj.getLabelName() + "已经存在");
				}
			}
		}
		baseEntityDataUtil.updateBaseEntityData(obj);
		int update = equipLabelConfigDao.updateById(obj);
		if (update < 1) {
			throw new RuntimeException("修改失败");
		}
		this.convertAndSend(obj);

		return resultBean;
	}

	public ResultBean delete(EquipLabelConfigEntity obj) {
		ResultBean resultBean = new ResultBean(true);
		if (obj == null) {
			throw new RuntimeException("数据为空");
		}
		EquipLabelConfigEntity equipDevEntity = equipLabelConfigDao.selectById(obj.getId());
		if (equipDevEntity == null) {
			throw new RuntimeException("删除的数据不存在！");
		}
		equipDevEntity.setRowStatus(0);
		int updateById = equipLabelConfigDao.updateById(equipDevEntity);
		if (updateById < 1) {
			throw new RuntimeException("删除失败！");
		}
        this.convertAndSend(obj);
		return resultBean;
	}

	public void convertAndSend(EquipLabelConfigEntity obj){
		EquipConnectConfigEntity con=equipConnectConfigDao.selectById(obj.getConnectId());
		EquipDataConfigEntity data=equipDataConfigDao.selectById(con.getDataId());
		SubscribeMessage subscribeMessage=new SubscribeMessage();
		subscribeMessage.setConnectId(obj.getConnectId());
		subscribeMessage.setDataId(data.getId());
		subscribeMessage.setNewIp(data.getServiceIp());
		subscribeMessage.setCrud(MomEquipRedisChannel.EQUIP_INFO_CHANNEL_UPDATE);
		stringRedisTemplate.convertAndSend(MomEquipRedisChannel.EQUIP_INFO_CHANNEL,JSONObject.toJSONString(subscribeMessage));

	}


	public ResultBean deleteByConnectIds(List<Long> connectIds) {
		ResultBean resultBean = new ResultBean(true);
		if (connectIds == null) {
			throw new RuntimeException("数据为空");
		}
		QueryWrapper<EquipLabelConfigEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.in("connect_id",connectIds);
		List<EquipLabelConfigEntity> equipDevEntity = equipLabelConfigDao.selectList(queryWrapper);
		if (equipDevEntity != null&&equipDevEntity.size()>0) {
			for(EquipLabelConfigEntity e:equipDevEntity){
				e.setRowStatus(0);
				baseEntityDataUtil.updateBaseEntityData(e);
			}
			this.updateBatchById(equipDevEntity);
		}
		return resultBean;
	}

	public EquipLabelConfigEntity getById(long id) {
		EquipLabelConfigEntity bean = equipLabelConfigDao.selectById(id);
		return bean;
	}

	public EquipLabelConfigParamBean selectBySomeFields(EquipLabelConfigParamBean obj) {
		if (obj.getPageNum() == null) {
			obj.setPageNum(1);
		}

		if (obj.getPageSize() == null) {
			obj.setPageSize(10);
		}
		QueryWrapper<EquipLabelConfigEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.orderByDesc("creation_time");
		if (StringUtils.isNotEmpty(obj.getLabelGroup())) {
			queryWrapper.like("label_group", obj.getLabelGroup());
		}
		if (StringUtils.isNotEmpty(obj.getLabelName())) {
			queryWrapper.like("label_name", obj.getLabelName());
		}
		if (StringUtils.isNotEmpty(obj.getLabelDesc())) {
			queryWrapper.like("label_desc", obj.getLabelDesc());
		}
		Page<EquipLabelConfigEntity> page = new Page<>(obj.getPageNum(), obj.getPageSize(), true);
		IPage<EquipLabelConfigEntity> selectPage = equipLabelConfigDao.selectPage(page, queryWrapper);

		obj.setEntityList(selectPage.getRecords());
		obj.setPageCount((int) selectPage.getPages());
		obj.setTotalQuantity((int) selectPage.getTotal());
		return obj;
	}
}
